//
//  SceneCell.m
//  OST
//
//  Created by mac on 13-7-15.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "SceneCell.h"
#import "GlobalVar.h"
#import "SetData.h"
#import "CommonData.h"

@interface SceneCell ()

@property (weak, nonatomic) IBOutlet UIButton *editButton;
@property (weak, nonatomic) IBOutlet UIButton *activateBtn;
@end

@implementation SceneCell
@synthesize labelName,icon;
@synthesize data;

-(void)initView
{
    UIImage *img1  = [UIImage imageNamed:@"bg_cell"];
    UIImage *img2  = [UIImage imageNamed:@"bg_cell_sel"];
    
    UIImageView *cellbg1 = [[UIImageView alloc]initWithImage:img1];
    UIImageView *cellbg2 = [[UIImageView alloc]initWithImage:img2];
    
    self.backgroundView = cellbg1;
    self.selectedBackgroundView = cellbg2;
}

-(void)createCommand:(NSString *)zid value:(NSString *)value
{
    NSDictionary *config = [GlobalVar getDataForKey:@"data.mac"];
    NSString *homeId = [config objectForKey:@"homeid"];
    NSString *key = [NSString stringWithFormat:@"%@%@",homeId,zid];
    
    NSLog(@"%@",key);
    
    
    NSArray *settings = [[SetData sharedInstance]getResultData];
    if (!settings) {
        return;
    }
    
    NSDictionary *itemconfig = nil;
    for (NSDictionary *item in settings) {
        NSString *itemzid = [item objectForKey:@"zid"];
        if ([itemzid isEqualToString:key]) {
            itemconfig = item;
            break;
        }
    }
    
    if (!itemconfig) {
        return;
    }
    
    NSString *kind = [[itemconfig objectForKey:@"kind"] lowercaseString];
    NSString *url = @"";
   
    if ([kind isEqualToString:@"binaryswitch"]) {
        url = [NSString stringWithFormat:@"cgi-bin/Switch.cgi?ZID=%@&OP=%@",key,value];
    }else if ([kind isEqualToString:@"multilevelswitch"]){
        int sensorId = [[itemconfig objectForKey:@"sensorid"]intValue];
        if (sensorId==1){
            int newValue = [value intValue];
            if (newValue == 0) {//off
                url = [NSString stringWithFormat:@"cgi-bin/Switch.cgi?ZID=%@&OP=%d&VALUE=%d",key,7,0];
            }else if (newValue == 1) {//on
                url = [NSString stringWithFormat:@"cgi-bin/Switch.cgi?ZID=%@&OP=%d&VALUE=%d",key,7,255];
            }
        }else {
            url = [NSString stringWithFormat:@"cgi-bin/Switch.cgi?ZID=%@&VALUE=%@",key,value];
        }
    }else if ([kind isEqualToString:@"avcontrolpoint"]){
        url = [NSString stringWithFormat:@"cgi-bin/command_class_AV.cgi?ZID=%@&CH=%@&COMMAND=AV_SET&Value=%d",key,value,39];
    }else if ([kind isEqualToString:@"thermostat"]){
        url = [NSString stringWithFormat:@"cgi-bin/command_class_thermostat_mode.cgi?ZID=%@&COMMAND=THERMOSTAT_MODE_SET&MODE=%d",key,5];
    }

    NSLog(@"%@",url);
    CommonData *commonData = [[CommonData alloc]initWidthUrl:url];
    commonData.justCommand = YES;
    [commonData getDataWithDataRecivedBlock:^(id xdata) {
        NSLog(@"%@",xdata);
    }];
    
}



-(IBAction)startScene:(id)sender
{
    NSString *value = [data objectForKey:@"value"];
    NSLog(@"%@",value);
    NSArray *items = [value componentsSeparatedByString:@"/./"];
    if ([items count]>1) {
        for (NSString *item in items) {
            NSArray *values = [item componentsSeparatedByString:@"="];
            if (values && [values count]>1) {
                NSString *suffixZID = [values objectAtIndex:0];
                NSString *value = [values objectAtIndex:1];
                [self createCommand:suffixZID value:value];
            }
        }
    }
}

- (IBAction)editScene:(id)sender {
    
    
}

- (void)editButtonAddTarget:(id)target action:(SEL)action {
    [self.editButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)activateButtonAddTarget:(id)target action:(SEL)action {
    
    [self.activateBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

-(IBAction)changeInfo:(id)sender
{
    //[self buttonSelectedWithSound];
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeInfo:cell:)])
    {
        [self.delegate changeInfo:self.data cell:self];
    }
}

@end
